package com.liu.blindBox.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.blindBox.entity.qo.BlindBoxInfoQO;
import com.liu.blindBox.entity.vo.BlindBoxInfoVO;
import com.liu.blindBox.entity.vo.BlindBoxTypeInfoVO;
import com.liu.blindBox.entity.vo.SalesSituationVO;
import com.liu.blindBox.service.MerchantBlindBoxService;
import com.liu.common.entity.Result;
import com.liu.common.entity.StatusCode;
import com.liu.common.service.GetTokenService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/merchant")
public class MerchantBlindBoxController {

    @Resource
    private MerchantBlindBoxService merchantBlindBoxService;

    @Resource
    private GetTokenService getTokenService;

    /**
     * 添加盲盒信息
     * @param blindBoxInfoQO
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/addBlindBox")
    public Result addBlindBox(@RequestBody BlindBoxInfoQO blindBoxInfoQO){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null){
            return new Result(false, StatusCode.ERROR,"添加失败,您还未登录");
        }else {
            blindBoxInfoQO.setUserId(String.valueOf(loginIdByToken));
            int i = merchantBlindBoxService.insertProduct(blindBoxInfoQO);
            if (i == 0){
                return new Result(false, StatusCode.ERROR,"添加失败,盲盒名称已存在");
            }
            if (i == 2){
                return new Result(false, StatusCode.ERROR,"添加失败,图片还没上传");
            }
            return new Result(true, StatusCode.OK,"添加成功");
        }
    }

    /**
     * 上传图片
     * @param image
     * @return
     * @throws IOException
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/addBlindBoxImage")
    public Result addBlindBoxImage(@RequestParam("image") MultipartFile image) throws IOException {
        merchantBlindBoxService.insertProductImage(image);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 更新盲盒信息
     * @param blindBoxInfoQO
     * @return
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @PostMapping("/updateBlindBox")
    public Result addBlindBoxImage(@RequestBody BlindBoxInfoQO blindBoxInfoQO){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null){
            return new Result(false, StatusCode.ERROR,"修改失败","您还未登录");
        }else {
            merchantBlindBoxService.updateBlindBoxInfo(blindBoxInfoQO);
            return new Result(true, StatusCode.OK,"修改成功");
        }
    }

    /**
     * 删除盲盒
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/deleteBlindBox")
    public Result deleteBlindBox(@RequestParam("blindBoxId") String blindBoxId){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            List<BlindBoxInfoVO> blindBoxInfoVOS = merchantBlindBoxService.queryBlindBoxInfoByUserId(String.valueOf(loginIdByToken),null);
            if (CollectionUtils.isEmpty(blindBoxInfoVOS)){
                return new Result(false, StatusCode.ERROR,"删除失败");
            }else {
                List<String> blindBoxIdList = blindBoxInfoVOS.stream().map(BlindBoxInfoVO::getBlindBoxId).collect(Collectors.toList());
                if (blindBoxIdList.contains(blindBoxId)){
                    merchantBlindBoxService.deleteBlindBox(String.valueOf(loginIdByToken), blindBoxId);
                    return new Result(true, StatusCode.OK,"下架成功");
                }
            }
        }
        return new Result(false, StatusCode.ERROR,"用户未登录");
    }

    /**
     * 查询销售情况
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/querySalesSituation")
    public Result querySalesSituation(@RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize,@RequestParam("key")String keyword){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            PageInfo<SalesSituationVO> salesSituationVOPageInfo = merchantBlindBoxService.querySalesSituation(String.valueOf(loginIdByToken), keyword, pageNum, pageSize);
            return new Result(true, StatusCode.OK,"查询成功",salesSituationVOPageInfo);
        }else {
            return new Result(false, StatusCode.ERROR,"查询失败,请登录商户用户");
        }

    }

    /**
     * 查询商户用户拥有的盲盒
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/queryMerchantBlindBox")
    public Result queryMerchantBlindBox(@RequestParam("pageNum")String pageNum, @RequestParam("pageSize")String pageSize,@RequestParam(value = "keyword", required = false)String keyword){
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            List<BlindBoxInfoVO> blindBoxInfoVOS = merchantBlindBoxService.queryBlindBoxInfoByUserId(String.valueOf(loginIdByToken),keyword);
            return new Result(true, StatusCode.OK,"查询成功",new PageInfo<>(blindBoxInfoVOS));
        }else {
            return new Result(false, StatusCode.ERROR,"查询失败，用户未登录");
        }
    }

    /**
     * 查询盲盒类型
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/queryBlindBoxType")
    public Result queryBlindBoxType(){
        List<BlindBoxTypeInfoVO> blindBoxTypeInfoVOS = merchantBlindBoxService.queryBlindBoxType();
        return new Result(true, StatusCode.OK,"查询成功",blindBoxTypeInfoVOS);
    }

    /**
     * 通过查询盲盒
     */
    @CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/queryBlindBoxInfo")
    public Result queryBlindBoxInfo(@RequestParam("blindBoxId") String blindBoxId){
        BlindBoxInfoQO blindBoxInfoQO = merchantBlindBoxService.queryBlindBoxInfo(blindBoxId);
        return new Result(true, StatusCode.OK,"查询成功",blindBoxInfoQO);
    }
}
